#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <errno.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <sys/types.h> 
#include <unistd.h> 
#include <sys/time.h> 
  
#define BUFLEN 1024 
#define PORT 5234
#define LISTNUM 20
  
int main() 
{ 
    int sockfd, newfd; 
    struct sockaddr_in s_addr, c_addr; 
    char buf[BUFLEN]; 
    socklen_t len; 
    unsigned int port, listnum; 
    fd_set rfds; 
    struct timeval tv; 
    int retval,maxfd; 
      

    if((sockfd = socket(AF_INET,SOCK_STREAM, 0)) == -1){ 
        perror("socket"); 
        exit(errno); 
    }else 
        printf("socket create success!\n"); 
    memset(&s_addr,0,sizeof(s_addr)); 
    s_addr.sin_family = AF_INET; 
    s_addr.sin_port = htons(PORT); 
    s_addr.sin_addr.s_addr = htons(INADDR_ANY); 
    
  
    if((bind(sockfd, (struct sockaddr*) &s_addr,sizeof(struct sockaddr))) == -1){ 
        perror("bind"); 
        exit(errno); 
    }else 
        printf("bind success!\n"); 

    if(listen(sockfd,listnum) == -1){ 
        perror("listen"); 
        exit(errno); 
    }else 
        printf("the server is listening!\n"); 
    while(1){ 
        printf("********************************\n"); 
        len = sizeof(struct sockaddr); 
        if((newfd = accept(sockfd,(struct sockaddr*) &c_addr, &len)) == -1){ 
            perror("accept"); 
            exit(errno); 
        }else 
            printf("%s: %d\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port)); 
        while(1){ 
            FD_ZERO(&rfds); 
            FD_SET(0, &rfds); 
            maxfd = 0; 
            FD_SET(newfd, &rfds); 
        
            if(maxfd < newfd) 
                maxfd = newfd; 
          
            tv.tv_sec = 6; 
            tv.tv_usec = 0; 
          
            retval = select(maxfd+1, &rfds, NULL, NULL, &tv); 
            if(retval == -1){ 
                printf("\n"); 
                break; 
            }else if(retval == 0){ 
                printf("waiting...\n"); 
                continue; 
            }else{ 
               
                if(FD_ISSET(0, &rfds)){ 
            
                
                    memset(buf,0,sizeof(buf)); 
                    
                    fgets(buf,BUFLEN,stdin); 
                    
                  
                    if(!strncasecmp(buf,"quit",4)){ 
                        printf("\n"); 
                        break; 
                    } 
                        len = send(newfd,buf,strlen(buf),0); 
                    if(len > 0) 
                        printf("%s\n",buf); 
                    else{ 
                        printf("\n"); 
                        break; 
                    } 
                } 
          
                if(FD_ISSET(newfd, &rfds)){ 
                
                    memset(buf,0,sizeof(buf)); 
                    
                    len = recv(newfd,buf,BUFLEN,0); 
                    if(len > 0) 
                        printf("%s\n",buf); 
                    else{ 
                        if(len < 0 ) 
                            printf("\n"); 
                        else 
                            printf("\n"); 
                        break; 
                    } 
                } 
            } 
        } 
         
        close(newfd); 
        
        printf(" "); 
        bzero(buf, BUFLEN); 
        fgets(buf,BUFLEN, stdin); 
        if(!strncasecmp(buf,"y",1)){ 
            printf("\n"); 
            break; 
        } 
    } 

    close(sockfd); 
    return 0; 
}
